package newcall.com.newcalldemo;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.m800.sdk.IM800Management;
import com.m800.sdk.M800Error;
import com.m800.sdk.M800SDK;

public class App extends MultiDexApplication {
    private M800SDK mM800SDK;
    @Override
    public void onCreate() {
        super.onCreate();
        new InitM800SDKTask(getApplicationContext()){

            @Override
            protected void onPostExecute(Boolean aBoolean) {
                super.onPostExecute(aBoolean);
              // Add connection listener
                if (!aBoolean)return;
                Log.d("test", "onPostExecute: 成功");
                mM800SDK = M800SDK.getInstance();
                mM800SDK.getManagement().addConnectionListener(mConnectionListener);
            }
        }.execute();
//        new InitM800SDKTask(getApplicationContext()) {
//            @Override
//            protected void onPostExecute(Boolean initialized) {
//                if (!initialized) {
//                    return;
//                }
//                mM800SDK = M800SDK.getInstance();
//                mCallSessionManager = M800CallSessionManager.getInstance();
//                mM800ConferenceManager = M800ConferenceManager.getInstance(getApplicationContext());
//
//                mChatMessageManager = mM800SDK.getChatMessageManager();
//                mSingleChatManager = mM800SDK.getSingleUserChatRoomManager();
//                mGroupChatManager = mM800SDK.getMultiUserChatRoomManager();
//                mSystemChatManager = mM800SDK.getSystemChatRoomManager();
//                mSMSChatRoomManager = mM800SDK.getSMSChatRoomManager();
//                mUserManager = mM800SDK.getUserManager();
//
//                // Add connection listener
//                mM800SDK.getManagement().addConnectionListener(mConnectionListener);
//
//                // Register contact listener
//                if (mUserManager != null) {
//                    mUserManager.addContactChangeListener(mContactListener);
//                }
//
//                //Register Conference Call Listener
//                mM800ConferenceManager.addListener(AppM800Service.this);
//
//                // Register chat message listener for all rooms
//                mChatMessageManager.addChatMessageListener(AppM800Service.this);
//
//                // Get chat room list
//                mChatRoomExecutor = Executors.newSingleThreadExecutor();
//                mChatRoomExecutor.execute(new GetChatRoomListTask());
//                mChatRoomItems = new ArrayList<>();
//                mEphemeralContextList = new ConcurrentHashMap<>();
//            }
//        }.execute();
    }

//    @Override
//    protected void attachBaseContext(Context base) {
//        super.attachBaseContext(base);
//        MultiDex.install(this);
//    }


    private IM800Management.M800ManagementConnectionListener mConnectionListener =
            new IM800Management.M800ManagementConnectionListener() {
                @Override
                public void onConnectedToM800() {
                    Log.d("test", "Connected to M800!");
//                    isConnectedToM800 = true;
//                    Intent intent = new Intent(ACTION_M800_CONNECTION_CHANGED);
//                    intent.putExtra(EXTRA_IS_CONNECTED, true);
//                    LocalBroadcastManager.getInstance(getApplicationContext())
//                            .sendBroadcast(intent);

//                    // Update the push tokens.
//                    new JPushService().updateJPushToken(getApplicationContext());
//                    GCMManager.getSharedInstance(getApplicationContext()).updatePushToken();
                }

                @Override
                public void onDisconnectedFromM800(M800Error error) {
                    Log.d("test", "Disconnected from M800, error msg: " + error.getMessage());
//                    isConnectedToM800 = false;
//                    if (mM800SDK.getManagement().needKickUserForError(error)) {
//                        Log.w(DEBUG_TAG, "Need kick user for error!");
//                        if (M800SDK.getInstance().hasUserSignedUp()) {
//                            new LogoutTask(getApplication(), AppM800Service.this).execute();
//                        }
//                    } else {
//                        Intent intent = new Intent(ACTION_M800_CONNECTION_CHANGED);
//                        intent.putExtra(EXTRA_IS_CONNECTED, false);
//                        LocalBroadcastManager.getInstance(getApplicationContext())
//                                .sendBroadcast(intent);
//                    }
                }
            };

}
